package my.lotu.dao;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import my.lotu.bean.Food;

/**
 * A data access object (DAO) providing persistence and search support for Food
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see my.lotu.bean.Food
 * @author MyEclipse Persistence Tools
 */
public class FoodDAO extends BasicDao {
	private static final Logger log = LoggerFactory.getLogger(FoodDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String TYPE1 = "type1";
	public static final String TYPE2 = "type2";
	public static final String PICTURE = "picture";
	public static final String PRICE = "price";
	public static final String PROTEIN = "protein";
	public static final String CARBOHYDRA = "carbohydra";
	public static final String FAT = "fat";
	public static final String CALORIE = "calorie";
	public static final String MASS = "mass";
	public static final String CULTURE = "culture";
	public static final String NATURE = "nature";

	private Session getSession() {
		return getSessionFactory().getCurrentSession();
	}
	
	public void save(Food transientInstance) {
		log.debug("saving Food instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public Food findById(java.lang.Integer id) {
		log.debug("getting Food instance with id: " + id);
		try {
			Food instance = (Food) getSession().get("my.lotu.bean.Food", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("deprecation")
	public List<?> findByExample(Food instance) {
		log.debug("finding Food instance by example");
		try {
			List<?> results = getSession().createCriteria("my.lotu.bean.Food").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("deprecation")
	public List<?> findAll() {
		log.debug("finding all Food instances");
		try {
			String queryString = "from Food";
			Query<?> queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}